/**
  ******************************************************************************
  * @file    Icons.c
  * @author  Qilong Huang
  * @version V0.0.1
  * @date    05-03-2020
  * @brief   This file contains functions enable/disable  icons
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/


#include "icons.h"
#include "Key.h"

U8 led_control = 0x00;
u32 icon_out_1 = 0;
u8 led_counter = 0;
u8 fix_led_counter = 0;
u8 bat_led_counter = 0;
u8 Buz_10Sec_flag = TRUE;
u8 Buz_5Sec_flag = TRUE;
int buz_counter = 0;
int buz_counter_0 = 0;
/**
* @brief	all led on
* @param	none
* @note     none
* @retval   none
*/
void All_Led_On(void)
{
	U8 temp_out = 0x00;
	temp_out = temp_out | LED_STOP_ON | LED_BATTTARYLOW_ON | LED_TEMPURATEHIGH_ON | LED_FIX_ON | LED_DRIVERON_ON;
    write_sl595(temp_out);
}
/**
* @brief	all led off 
* @param	none
* @note     none
* @retval   none
*/
void All_Led_Off(void)
{
    led_control = 0;
    write_sl595(0);
}


/**
* @brief	write s595, digital output to control led or others 
* @param	u16 data: output data
* @note     none
* @retval   none
*/
void write_sl595(u32 data)
{
    u16 i, data1temp, value;
    
    value = data & 0xFF;
    GPIO_ResetBits(IC595_LATCH_PORT, IC595LATCH_PIN);
    for(i = 0; i < DIG_1_OUTPUTS_NUM; i++)   
    {
        GPIO_ResetBits(DIG_OUTPUT_PORTS, CHIP_1_CLK_PIN);
        Delay(5);
        data1temp = (value >> i) & 0x01;
        if(data1temp == 0x00)
        {
            GPIO_SetBits(DIG_OUTPUT_PORTS, CHIP_1_DATA_PIN);
        }
        else
        {
            GPIO_ResetBits(DIG_OUTPUT_PORTS, CHIP_1_DATA_PIN);
        }
        Delay(5);
        GPIO_SetBits(DIG_OUTPUT_PORTS, CHIP_1_CLK_PIN);
        Delay(5);
    }
    GPIO_SetBits(IC595_LATCH_PORT, IC595LATCH_PIN);
}
/**
* @brief	This task updates the status of the Led Icons based on the configured 
*           inputs (switches, CAN, etc.)
* @param	void const *argument
* @note     none
* @retval   none
*/
void ledicons_task(void const *argument) 
{
    u32 temp_out = 0;
    // Initialization 
    task_status[UPDT_LEDICONS_ID] = ALIVE;
    // Tasks must run in an infinite loop:
	GPIO_ResetBits(DIG_OUTPUT_PORTS, CHIP_1_OUTPUT_EN_PIN);            //enable 1595 cs pin
    while (TRUE)
    {
        // temp_out = led_control & (~REVERSE);
        // write_sl595(temp_out);
        // task_status[UPDT_LEDICONS_ID] = ALIVE;
        if(Screen_Mode == MODE_Welcome)
        {
            if(welcom_mode_counter < 3)
            {
                All_Led_On();               
            }
            else if(welcom_mode_counter < 5)
            {
                All_Led_Off();
            }
            else if(welcom_mode_counter < 8)
            {
                Buzzer_ON();
                All_Led_On();
            }
            else if(welcom_mode_counter < 10)
            {
                All_Led_Off();
                Buzzer_OFF();                
            }     
        }
        else if((Screen_Mode == MODE_PinCode) || (Screen_Mode == MODE_ProInfo))
        {
            Buzzer_OFF();
            All_Led_Off();
        }
        else if(Screen_Mode == MODE_Standby)
        {
            All_Led_Off();
            Buzzer_OFF();  
        }
        else
		{
			temp_out = 0x00;
            /*Tempurature too high LED On*/
		    if(((BR1189_lift_status.motor_over_tempurate == TEMPURATE_OVER) || (BR1189_steer_status.motor_over_tempurate == TEMPURATE_OVER)\
				|| (BR1189_trac_status.motor_over_tempurate == TEMPURATE_OVER) || (BR1189_lift_status.fets_over_tempurate == TEMPURATE_OVER)\
			    || (BR1189_steer_status.fets_over_tempurate == TEMPURATE_OVER) || (BR1189_trac_status.fets_over_tempurate == TEMPURATE_OVER)) && (TempurateErrCodeDealFlag == TRUE))
			{
			    temp_out = temp_out | LED_TEMPURATEHIGH_ON;
			}
			else
			{
			    temp_out = temp_out & (~LED_TEMPURATEHIGH_ON);
			}
            /*Battery lock LED on*/
		    if(BR1189_trac_status.battery_lock == BAT_LOCK)
			{
			    temp_out = temp_out | LED_BATTTARYLOW_ON;
			}
			else
			{
			    temp_out = temp_out & (~LED_BATTTARYLOW_ON);
			}
            /*Oil lack LED om*/
		    if(((BR1189_trac_status.active_oil_level == OIL_LACK) || (BR1189_lift_status.pump_oil_level == OIL_LACK))&& (OilErrCodeDealFlag == TRUE))
			{
			    temp_out = temp_out | LED_STOP_ON;
			}
			else
			{
			    temp_out = temp_out & (~LED_STOP_ON);
			}
            /*Platform switch LED on*/
            if(BR1189_trac_status.foot_platfm_sw == SW_NOTPRESSED)
            {
                led_counter ++;
                if(led_counter >= 10)
                {
                    temp_out = temp_out | LED_DRIVERON_ON;
                }
                if(led_counter >= 20)
                {
                    led_counter = 0;
                    temp_out = temp_out & (~LED_DRIVERON_ON);
                }
            }
            else
            {                
                if(led_counter <= 200)
                {
                    led_counter ++;
                    temp_out = temp_out | LED_DRIVERON_ON;
                }
                else
                {
                    temp_out = temp_out & (~LED_DRIVERON_ON);
                }
            }
            /*Matiance time warning LED*/
            if(PreMatinFlag == 1)
            {
                fix_led_counter ++;
                if(fix_led_counter >= 10)
                {
                    temp_out = temp_out | LED_FIX_ON;
                }
                if(fix_led_counter >= 20)
                {
                    fix_led_counter = 0;
                    temp_out = temp_out & (~LED_FIX_ON);
                }            
            }              
            else
            {
                temp_out = temp_out & (~LED_FIX_ON);
            }    
            if((Current_Bat_Soc <= CutOff) || (Current_Bat_Soc <= 10))
            {
                bat_led_counter ++;
                if(bat_led_counter >= 10)
                {
                    temp_out = temp_out | LED_BATTTARYLOW_ON;
                }
                if(bat_led_counter >= 20)
                {
                    bat_led_counter = 0;
                    temp_out = temp_out & (~LED_BATTTARYLOW_ON);
                }         
            }
			write_sl595(temp_out);//Refresh LED status
            if(BR1189_trac_status.buzzer_warning == BUZZER_ON)
            {
                Buzzer_ON();
            }
            else
            {
                Buzzer_OFF();
            }         
            if(Current_Bat_Soc > 10)
            {
                Buz_10Sec_flag = TRUE;
            }
            if(Current_Bat_Soc > 5)
            {
                Buz_5Sec_flag = TRUE;
            }
            if((Current_Bat_Soc == 10) && (Buz_10Sec_flag == TRUE))
            {
                buz_counter ++;
                if(buz_counter <= 10)
                {
                    Buzzer_ON();
                }
                else if(buz_counter <= 20)
                {
                    Buzzer_OFF();
                }
                else if(buz_counter <= 30)
                {
                    Buzzer_ON();
                }
                else if(buz_counter <= 40)
                {
                    Buzzer_OFF();
                }           
                else if(buz_counter <= 50)
                {
                    Buzzer_ON();
                }          
                else if(buz_counter <= 60)
                {
                    buz_counter = 0;
                    Buz_10Sec_flag = FALSE;
                    Buzzer_OFF();
                }                      
            }       
            if((Current_Bat_Soc == 5) && (Buz_5Sec_flag == TRUE))
            {
                buz_counter ++;
                if(buz_counter <= 10)
                {
                    Buzzer_ON();
                }
                else if(buz_counter <= 20)
                {
                    Buzzer_OFF();
                }
                else if(buz_counter <= 30)
                {
                    Buzzer_ON();
                }
                else if(buz_counter <= 40)
                {
                    Buzzer_OFF();
                }           
                else if(buz_counter <= 50)
                {
                    Buzzer_ON();
                }          
                else if(buz_counter <= 60)
                {
                    buz_counter = 0;
                    Buz_5Sec_flag = FALSE;
                    Buzzer_OFF();
                }          
            }       
            if(Current_Bat_Soc == 0)
            {
                buz_counter_0 ++;
                if(buz_counter_0 <= 10)
                {
                    Buzzer_ON();
                }
                else if(buz_counter_0 <= 20)
                {
                    Buzzer_OFF();
                }
                else if(buz_counter_0 <= 30)
                {
                    Buzzer_ON();
                }
                else if(buz_counter_0 <= 40)
                {
                    Buzzer_OFF();
                }           
                else if(buz_counter_0 <= 50)
                {
                    Buzzer_ON();
                }          
                else if(buz_counter_0 <= 60)
                {
                    Buzzer_OFF();
                }   
                if(buz_counter_0 >= 600)
                {
                    buz_counter_0 = 0;
                }               
            }
            if(SoftCloseFlag == TRUE)
            {
                Buzzer_ON();
            }
		}
        task_status[UPDT_LEDICONS_ID] = ALIVE;
        osDelay(UPDT_LEDICONS_ITV);
    }  // end while infinite loop
}  // end task_updt_icons()

